<template>
    <div>
        <el-card class="xj-table-list">
            <avue-crud ref="crudSysUserAkSk"
                       :data="data"
                       :option="option"
                       v-model:page="page"
                       v-model:search="search"
                       :table-loading="loading"
                       :cell-style="cellStyle"
                       @on-load="onLoad"
                       @refresh-change="onLoad"
                       @search-change="searchChange"
                       @row-click="handleRowClick">
                <!-- 启用/禁用插槽(默认提供,按需使用) -->
                <template #disable="{size,row}">
                    <el-switch v-model="row.disable" @change="updDisable(row)"
                               :active-value=0 :inactive-value=1
                               active-text="" inactive-text="">
                    </el-switch>
                </template>
                <template #expireTime-search="{size}">
                    <div class="block">
                        <el-date-picker
                            v-model="search.expireTime"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            type="datetimerange"
                            :shortcuts="defaultdata.timeOptions.shortcuts"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
                        </el-date-picker>
                    </div>
                </template>
                <template #menu-left="{size}">
                    <span><!--防抖动--></span>
                    <el-button type="primary" :size="size" plain @click="addDialogVisible = true">生成 AK/SK</el-button>
                    <span style="color: var(--el-color-primary);margin-left: 10px"> 生成 AK/SK 后, 在请求头中传递 ACCESS_KEY/ SECRET_KEY 即可访问该用户拥有角色的接口资源权限</span>
                </template>
                <template #menu="{scope,row,size,index}">
                    <el-button type="primary" :size="size" text icon="el-icon-edit" @click="updRow(row,1)">编辑</el-button>
                    <el-button type="primary" :size="size" text icon="el-icon-delete" @click="rowDel(row,index)">删除</el-button>
                </template>
            </avue-crud>
            <!-- 弹层 -->
            <el-dialog title="生成 AK/SK" draggable v-if="addDialogVisible" v-model="addDialogVisible" :width="dialogWidth" top="15vh" @close="closeDialog">
                <Add @closeDialog="closeDialog" :userId="search.userId" :uri="uri"></Add>
                <span slot="footer" class="dialog-footer"></span>
            </el-dialog>
            <el-dialog title="编辑" draggable v-if="updDialogVisible" v-model="updDialogVisible" :width="dialogWidth" top="15vh" @close="closeDialog">
                <Upd @closeDialog="closeDialog" :uri="uri" :rowData="rowData"></Upd>
                <span slot="footer" class="dialog-footer"></span>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import {defineAsyncComponent} from 'vue';

export default {
    components: {
        Add: defineAsyncComponent(() => import('./userAkSkAdd.vue')),
        Upd: defineAsyncComponent(() => import('./userAkSkUpd.vue'))
    },
    data() {
        return {
            uri: {
                infoList: "/api/admin/sys/userAkSk/findPage",
                info: "/api/admin/sys/userAkSk",
                sysUserList: "/api/admin/sys/user/list/keyData", // 系统用户查询接口
            },
            loading: true,
            dialogWidth: "60%",
            addDialogVisible: false,
            updDialogVisible: false,
            page: this.website.pageParams,
            search: {
                userId: this.userData.id
            },
            data: [],
            rowData: {},
            option: {},
            sysUsers: {},
        }
    },
    // 接收值父组件传递值 (用户管理中打开)
    props: {
        // 选中用户
        userData: {},
    },
    async mounted() {
        await this.findSysUsers();
        this.option = JSON.parse(JSON.stringify(this.website.optionConfig));
        this.option.column = [
            {
                label: '用户',
                prop: 'userId',
                search: true,
                searchSpan: 5,
                overHidden: true,
                filterable: true,
                type: 'select',
                dicData: this.sysUsers,  // xxxDic 中的的字段映射
                props: {
                    label: "fullName",
                    value: "id"
                }
            },
            {
                label: 'AccessKey',
                prop: 'accessKey',
                search: true,
                searchSpan: 5,
                overHidden: true,
            },
            {
                label: 'SecretKey',
                prop: 'secretKey',
                search: false,
                searchSpan: 5,
                overHidden: true,
            },
            {
                label: '过期时间',
                prop: 'expireTime',
                search: true,
                searchSpan: 10,
                overHidden: true,
            },
            {
                label: '备注',
                prop: 'remarks',
                search: false,
                searchSpan: 5,
                overHidden: true,
            },

        ]
    },
    created() {
    },
    activated: function () {
        this.crud.doLayout(this, this.$refs.crudSysUserAkSk)
    },
    watch: {
        "search.userId": function (newVal, oldVal) {
            this.onLoad()
        }
    },
    methods: {
        onLoad() {
            this.crud.list(this, true);
            this.crud.doLayout(this, this.$refs.crudSysUserAkSk)
        },
        searchChange(params, done) {
            console.debug(params)
            this.page.currentPage = 1;
            this.onLoad();
            done();
        },
        closeDialog(isRefresh) {
            this.addDialogVisible = false;
            this.updDialogVisible = false;
            this.rowData = {};
            if (isRefresh != null && isRefresh) {
                this.onLoad();
            }
        },
        updRow(row, type) {
            this.rowData = row;
            switch (type) {
                case 1:
                    this.updDialogVisible = true;
                    break;
                default:
                    this.$message.error('操作类型错误');
                    break;
            }
        },
        rowDel(row, index) {
            this.crud.delRow(this, this.uri.info, row.id, index);
        },
        // 启用/禁用
        updDisable(row) {
            if (row.id != null) {
                this.crud.put(this.uri.info + "/" + row.id, {disable: row.disable});
            }
        },
        // 点击保存行数据(供行操作的任意地方获取数据)
        handleRowClick(row) {
            this.rowData = JSON.parse(JSON.stringify(row));
        },
        cellStyle({row, column}) {
            if (column.property == "disable") {
                // fontWeight: 'bold',fontSize: '20'
                return row.disable == 0 ? {color: 'green'} : {color: 'red'}
            }
        },
        // 查询系统人员
        async findSysUsers() {
            let res = await this.crud.get(this.uri.sysUserList);
            this.sysUsers = res.data.data;
        },
    }
}
</script>
